
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: FL87MPRYOA0R - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Auto Classifieds
 *	DOMAIN: www.siyaradiali.com
 *	FILE: LIB.JS
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

$(document).ready(function(){
	$('input[name=site_accounts]').click(function(){
		$('input.accounts').attr('checked', $(this).is(':checked'));
	});
	$('input.accounts').click(function(){
		$('input[name=site_accounts]').attr('checked', $('input.accounts:checked').length > 0 ? true : false);
	});
	
	$('input#confirm').click(function(){
		flynax.confirm(massmailer.phrases['send_confirm'], massmailer.save, null);
	});
	
	$('input#start_send').click(function(){
		flynax.confirm(massmailer.phrases['send_confirm'], massmailer.start, null);
	});
});

var massmailerClass = function(){
	var self = this;
	var item_width = width = percent = percent_value = 0;
	var window = false;
	var request;
	
	this.phrases = new Array();
	this.config = new Array();
	
	this.save = function(){
		$('input#confirm').val(lang['loading']);
		
		var account_types = new Array();
		$('input.accounts:checked').each(function(){
			account_types.push($(this).val());
		});
		
		xajax_massmailerSave(
			self.config['id'],
			$('input[name=from_mail]').val(),
			$('input[name=status]').val(),
			$('input[name=subject]').val(),
			CKEDITOR.instances['body'].getData(),
			$('input[name=newsletters_accounts]').is(':checked') ? 1 : 0,
			account_types.join(','),
			$('input[name=contact_us]').is(':checked') ? 1 : 0
		);
	}
		
	this.send = function(id, index){
		/* show window */
		if ( index == 0 )
		{
			if ( !window )
			{
				window = new Ext.Window({
					applyTo: 'statistic',
					layout: 'fit',
					width: 447,
					height: 140,
					closeAction: 'hide',
					plain: true
			    });
			    
			    window.addListener('hide', function(){
	            	self.stop();
	            });
			}
		    
			window.show();
		}
		
	    /* send request */
	    request = $.getJSON("../plugins/massmailer_newsletter/admin/send.php", {id: id, index: index}, function(response){
			if ( index == 0 )
			{
				item_width = Math.ceil(362/response['count']);
				percent_value = Math.ceil(100/response['count']);
			}
			
			index++;
			
			width += item_width;
			percent = response['count'] == index ? 100 : percent + percent_value;
			
			$('#total').html(response['count']);
			$('#sent').html(index);
			$('#processing').css('width', width+'px');
			$('#loading_percent').html(percent+'%');
			$('#sending').html(response['data']['Mail']);
			
			if ( response['count'] > index )
			{
				self.send(id, index);
			}
			else
			{
				printMessage('notice', self.phrases['completed'].replace('{count}', response['count']));
				setTimeout(function(){
					window.hide();
					self.clear();
				}, 4000);
			}
		});
	}
	
	this.stop = function(){
		request.abort();
	}
	
	this.start = function(){
		self.send(self.config['id'], 0);
	}
	
	this.clear = function(){
		item_width = width = percent = percent_value = 0;
		$('#total').html(0);
		$('#sent').html(0);
		$('#processing').css('width', '0px');
		$('#loading_percent').html('0%');
		$('#sending').html('-');
	}
}

var massmailer = new massmailerClass();